/**
 * 功能：
 * 	使视图与相关事件相分离
 *  类似于action 用来传递数据和控制视图
 *  quicktips 没有完成
 *  
 *  handler 事件需要重新改变量
 */
Ext.define('Risk.controller.ru.RuController',{
	extend : 'Ext.app.Controller',
	 stores:[
	   'ru.Pu6Store',
	   'ru.Pu4Store',
	   'ru.Pu2Store',
	   'ru.Pu1Store',
	   'ru.CtStore',
	   'ru.RuStore'
	 ],
	 models:[
	 	
	 ],
	      
	      /**
	       * 视图在congtroller中进行注册
	       * 将控制权转交给 this Controller
	       */
	  	  //管理的视图
     views: [
            'ru.Al',
            'ru.Pu',
            'ru.Ru',
            'ru.CtAndRu',
            'RuLinePanle'
    ],
    // 类创建好后要做的初始化工作
	init : function() {
	  this.control({
		  //类似于JQuery中的选择器   fieldLabel
	  	' alfieldSet > radiogroup':{
			  change: this.alChangeEvent
		  },
		  'alfieldSet > fieldcontainer > textfield':{
  		  //al选择事件
		  change: this.alNuberfieldChangeEvent
		  },
		  'pufieldSet > radiogroup':{
		  	//判断是否有遮盖物
			 change:this.puChangeEvent
		  },
		  'pufieldSet > fieldcontainer > radio':{
		  		//打开Combo
				 change:this.radioEvent
			  },
		'pufieldSet > fieldcontainer > combo':{
			//设置combox中值到相应的field中
			change:this.comboEvent
		 },
		 'ctandru > fieldcontainer combo':{
		 	//ru和ct属性设置
			change:this.comboEvent2
		 },
		 'rufieldset  > combo':{
			 change:this.comboEvent3
		 },
		'rupower':{
			 activate: this.getData1
		},
		'ruline':{
			 activate: this.getData2
		}
	  });
	},
	radioEvent: function(obj)
	{
		//TODO name改变 会更随改变
		//排除干扰项
		if(obj.name=='puRadio')return;
		//当选中的时候才进行该操作
		var flag = obj.up('pufieldSet').items.get(0).down('radio[checked=true]').inputValue==1;
		if(flag)obj.ownerCt.down('combo').setDisabled(!obj.value);
		
	},
	comboEvent:function(obj,newValue,oldValue)
	{
		obj.up('pufieldSet').down('#pu').setValue(obj.value);
	},
	comboEvent2:function(obj,newValue,oldValue,newValue,oldValue)
	{
		obj.ownerCt.down('#ct').setValue(obj.value);
	},
	comboEvent3:function(obj,newValue,oldValue)
	{
		obj.ownerCt.down('#ru').setValue(obj.value);
	},
	alNuberfieldChangeEvent:function(obj)
	{
		//计算Al和Ai值
		var ai = 0;
		var al = 0;
		var type = obj.up('alfieldSet').down('radio[checked=true][name=way]').inputValue;
		var Hc = parseFloat(obj.up('alfieldSet').down('#Hc').value);
		try{
			var Ha = parseFloat(obj.up('alfieldSet').down('#Ha').value);
			var Hb = parseFloat(obj.up('alfieldSet').down('#Hb').value);
			var Lc = parseFloat(obj.up('alfieldSet').down('#Lc').getValue());
			var p  = parseFloat(obj.up('alfieldSet').down('#p').value);
		}catch(e){return;}
		
		if(isNaN(Lc))Lc = 1000;
		//alert(Lc);
		if(type==1)
		{
			al =  6*Hc*(Lc-(Ha+Hb));
			ai = 1000*Lc;
		}else
		{
			al = (Lc - 3*(Ha+Hb))*Math.pow(p,0.5);
			ai = 25*Lc*Math.pow(p,0.5);
		}
		
		obj.up('alfieldSet').down('#ai').setValue(ai);
		obj.up('alfieldSet').down('#al').setValue(al);
		
	},
	puChangeEvent:function(obj,newValue,oldValue)
	{
		  var val = 0;
		  for(var x in newValue){val = newValue[x];}
		  if(!(val == 1))obj.up('pufieldSet').down('#pu').setValue(0);
		  var o = null;
		  for(var i=1; i<=4; i++)
		  {
		  	//禁用全部combo
			  obj.up('pufieldSet').down('#ct'+i).down("#combo"+(i-1)).setDisabled(true);
			  var temp = obj.up('pufieldSet').down('#ct'+i).down('radio[checked=true]');
			  //找出要打开的Combo
			  if(!(temp ==null || temp == undefined))
			  {	o = obj.up('pufieldSet').down('#ct'+i);}
		  }
		  
		  //打开选中的combo
		  if(val==1&& o!=null)
		  {
		  	o.down("combo").setDisabled(false);
		  	obj.up('pufieldSet').down('#pu').setValue(o.down("combo").value);
		  }
		  
	},
   	alChangeEvent:function(obj,newValue,oldValue)
   	{
		  //lc是否确定
		  var val = 0;
		  for(var x in newValue)
			  val = newValue[x];
		  
		 if(obj.fieldLabel==undefined||obj.fieldLabel=='')
		 {
			 	if(!(val == 1)) obj.up('alfieldSet').down("#Lc").setValue(1000);
				 obj.up('alfieldSet').down("#Lc").setDisabled(!(val == 1));
		 }else//架空or埋地
		 {
				 obj.up('alfieldSet').down("#p").setDisabled(val == 1);
				 obj.up('alfieldSet').down("#Hc").setDisabled(!(val == 1));
		 }
		 
		 this.alNuberfieldChangeEvent(obj);
	  },
	  getData1:function(obj)
		{
			var record = obj.up("maintab").ownerCt.ownerCt.items.get(0).getSelectionModel().selected.items[0];
			if(!obj.isLoad){
				obj.isLoad = true;
				Ext.Ajax.request({
				    url:'ru/get1.do' ,
				    params:{projid:record.data.id,_doc:new Date().getMilliseconds()},
				    success:function(response){
				    //成功操作form 注入数据
				    	
				    	var result = Ext.JSON.decode(response.responseText).success;
				    	if(result)
					    {
					    	var o = Ext.JSON.decode(response.responseText).others;
					    	obj.getForm().setValues(o);
					    	var comb = obj.down("pufieldSet").down('radio[checked=true][name=kv]').ownerCt.items.get(1);
					    	comb.setValue(o.pu);
				    	}

				    },
				    failure:function(response){
				     // 失败操作from 
				    	//obj.getForm().reset();
				    }
				});
			}
			
		},
		 getData2:function(obj)
			{
				var record = obj.up("maintab").ownerCt.ownerCt.items.get(0).getSelectionModel().selected.items[0];
				if(!obj.isLoad){
					obj.isLoad = true;
					Ext.Ajax.request({
					    url:'ru/get2.do' ,
					    params:{projid:record.data.id,_doc:new Date().getMilliseconds()},
					    success:function(response){
					    //成功操作form 注入数据
					    	
					    	var result = Ext.JSON.decode(response.responseText).success;
					    	if(result)
					    	{
						    	var o = Ext.JSON.decode(response.responseText).others;
						    	obj.getForm().setValues(o);
						    	//对于cobox中的单独设置值
						    	var comb = obj.down("pufieldSet").down('radio[checked=true][name=kv]').ownerCt.items.get(1);
						    	comb.setValue(o.pu);
					    	}else
					    	{
					    		//obj.getForm().reset();
					    	}
					    },
					    failure:function(response){
					     // 失败操作from 
					    	//obj.getForm().reset();
					    }
					});
				}
				
			}
	  
});